/**
 * Huisou.com Inc.
 * Copyright (c) 2011-2012 All Rights Reserved.
 */

package com.lyqg.action;

import static com.lyqg.Constants.SESSION_USER;

import java.util.Date;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.util.WebUtils;

import com.core.Constants;
import com.core.utils.WebUtil;
import com.lyqg.biz.IMessageService;
import com.lyqg.biz.IUserService;
import com.lyqg.domain.Message;
import com.lyqg.domain.OperatePoint;
import com.lyqg.domain.User;

/**
 * @description
 * 
 * @author chenzehe
 * @email hljuczh@163.com
 * @create 2012-8-14 下午08:14:05
 */
@Controller
public class MessageAction extends BaseAction {
	@Resource
	IMessageService messageService;
	@Resource
	IUserService userService;

	@RequestMapping(value = "/message/save.html")
	@ResponseBody
	public String saveMessage(HttpServletRequest request, ModelMap model) {
		try {
			Message message = new Message();
			message.setMessage(request.getParameter("comment"));
			message.setType(WebUtil.getInt(request, "comment_type"));
			message.setContentId(WebUtil.getInt(request, "comment_post_ID", 0));
			if (StringUtils.isBlank(message.getMessage())
					|| message.getContentId() == 0) {
				return "{\"result\":\"no\"}";
			}
			message.setIssueTime(new Date());
			User user = (User) WebUtils.getSessionAttribute(request,
					SESSION_USER);
			if (user != null) {
				message.setUserId(user.getId());
			} else {
				return "{\"result\":\"no\"}";
			}
			message.setStatus(Constants.STATUS_NORMAL);
			message.setCai(0);
			message.setDing(0);
			messageService.save(message);
			if (user != null) {
				userService.operatePoint(user.getId(), OperatePoint.COMMENT);
			}
			return "{\"result\":\"ok\"}";

		} catch (Exception e) {
			return "{\"result\":\"no\"}";
		}
	}

	@RequestMapping(value = "/updateding.html")
	@ResponseBody
	public String updateDing(HttpServletRequest request, ModelMap model) {
		try {
			int id = WebUtil.getInt(request, "id");
			messageService.updateDing(id);
			User user = (User) WebUtils.getSessionAttribute(request,
					SESSION_USER);
			if (user != null) {
				userService.operatePoint(user.getId(), OperatePoint.LIKE);
			}
			return "{\"result\":\"ok\"}";
		} catch (Exception e) {
			return "{\"result\":\"no\"}";
		}
	}

	@RequestMapping(value = "/updatecai.html")
	@ResponseBody
	public String updateCai(HttpServletRequest request, ModelMap model) {
		try {
			int id = WebUtil.getInt(request, "id");
			messageService.updateCai(id);
			User user = (User) WebUtils.getSessionAttribute(request,
					SESSION_USER);
			if (user != null) {
				userService.operatePoint(user.getId(), OperatePoint.LIKE);
			}
			return "{\"result\":\"ok\"}";
		} catch (Exception e) {
			return "{\"result\":\"no\"}";
		}
	}
}
